import * as patrolService from '../../services/patrol';
import {queryAssetType} from '../../services/assetType';
import {qryAllUser} from '../../services/user';
import {queryRoomList} from '../../services/tRoom';
import moment from 'moment'

export default {
  namespace: 'patrolPlan',
  state: {
    list: [],  
    roomList:[], 
    userList:[],  
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    modalType : 'create',
    newKey : '', 
    modalViewVisible:false,
    patrolModalVisible: false,
    detailList:[],
    assetCurrent:1,
    assetCurrentSize : 10,
    assetTotal : 0, 
    patrolList:[],
    patrolCurrent:1,
    patrolCurrentSize : 10,
    patrolTotal : 0, 
    patrolItem:{},
    currentItem:{},

    cycletime:{
      cycleType: 1,
      patrolPlanTimes:[],
    },
  },
  reducers: {
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    upState(state, { payload}) {
      return { ...state, ...payload };
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },        
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },        
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, cycletime:{ cycleType: 1, patrolPlanTimes:[],},newKey : new Date().getTime() + '' }
    },
    showPatrolModal (state, { payload }) {
        return { ...state, ...payload,  patrolModalVisible: true }
    },
    hidePatrolModal (state) {
      return { ...state, patrolModalVisible: false,}
    },  
    showViewModal (state) {
      return { ...state, modalViewVisible: true}
    },
    hideViewModal (state, { payload }) {
      return { ...state,...payload, modalViewVisible: false}
    },
    searchAssetSuc(state, { payload }) {
      return { ...state, ...payload };
    },   
    getPatrolListSuc(state, { payload }) {
      return { ...state, ...payload };
    }, 
    qryPatrolByIdSuc(state, { payload }) {
      return { ...state, ...payload };
    },   
  },
  effects: {    
    *queryUser({ payload },{ call, put }) {    
      const { body }  = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryRoomList({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    }, 
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(patrolService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    
    *create({ payload },{ call, put }){
      const { body } = yield call(patrolService.create,payload);
       yield put({ type: 'hideModal'});
       yield put({ type: 'reload' ,payload:{}});
    },
    *qryById({ payload },{ call, put }){
      const { body } = yield call(patrolService.qryById,payload.id_);
       if(body){
         var list = [];
         body.data.patrolPlanTimes.map(d=>{
           var dr ={
             ...d,
           };
          dr.startTime = new moment(dr.startTime,"HH:mm");
          dr.endTime = new moment(dr.endTime,"HH:mm");
          list.push(dr);
         });
        yield put({
          type: 'showModal',
          payload: {
            currentItem: body.data,
            modalType: 'edit',
            title: '编辑巡检计划',
            cycletime:{
              cycleType: body.data.cycleType,
              patrolPlanTimes:list,
          },
          },
      });
     }
    },  

    //查看巡检计划下的巡检单
    *viewPatrolDetail({ payload },{ call, put }){ 
       const { body } = yield call(patrolService.getPatrolList,payload);
       yield put({ type: 'getPatrolListSuc', payload: { patrolList:body.data,inspectPlanId:payload.inspectPlanId, patrolCurrent:body.current, patrolCurrentSize:body.size, patrolTotal:body.iTotalRecords}});
       yield put({ type: 'showPatrolModal'});
    },    
    //查看巡检计划下的巡检单
    *getPatrolList({ payload },{ call, put }) {
      const { body } = yield call(patrolService.getPatrolList,payload);
      yield put({ type: 'getPatrolListSuc', payload: { patrolList:body.data,inspectPlanId:payload.inspectPlanId, patrolCurrent:body.current, patrolCurrentSize:body.size, patrolTotal:body.iTotalRecords}});
    },
    //查看巡检计划下的巡检单结果
    *getPatrolResult({ payload },{ call, put }) {
      yield put({ type: 'showViewModal'});
      const { body } = yield call(patrolService.qryPatrolById,payload);
      yield put({ 
        type: 'qryPatrolByIdSuc', 
        payload: { 
            patrolItem:body.data,
          },
      });
      yield put({ type: 'getPatrolResultList',payload:payload});      
    },
    //查看巡检计划下的巡检单
    *getPatrolResultList({ payload },{ call, put }) {
      const { body } = yield call(patrolService.getPatrolResultList,{patrolId:payload});
      yield put({ type: 'getPatrolListSuc', payload: { detailList:body.data,patrolId:payload, assetCurrent:body.current, assetCurrentSize:body.size, assetTotal:body.iTotalRecords}});
    },
 
    *edit({ payload },{ call, put }){
      const { body } = yield call(patrolService.edit,payload);
      yield put({ type: 'hideModal'});
      yield put({ type: 'reload' ,payload:{}});
    }, 
    *remove({ payload },{ call, put }){
      const { body } = yield call(patrolService.remove,payload.id);
      yield put({ type: 'reload' ,payload:payload.search});
    },  
    *reload({ payload },{ call, put }){
       yield put({ type: 'queryList',payload});
    },      
   
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/patrolPlan') {
          dispatch({ type: 'queryList', payload:{ 'name':'' }});
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryRoomList' });          
         
        }
      });
    },
  },
};
